package com.harmontronics.hanma.paas.paas.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.harmontronics.hanma.paas.paas.annotation.Log;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmModelPublish;
import com.harmontronics.hanma.paas.paas.dto.input.IdInput;
import com.harmontronics.hanma.paas.paas.dto.input.PublishByMultiVersionInput;
import com.harmontronics.hanma.paas.paas.dto.input.PublishByPublishInput;
import com.harmontronics.hanma.paas.paas.dto.input.PublishByScriptInput;
import com.harmontronics.hanma.paas.paas.dto.output.HmModelPublishResult;
import com.harmontronics.hanma.paas.paas.service.HmModelPublishAppService;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 实体模型发布记录
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-05-11 09:24:43
 * @since v1.1-sprint1
 */
@RestController
@RequestMapping("/api/hm-model-publish")
@Api(tags = {"实体模型发布记录"})
public class HmModelPublishController {
    @Autowired
    private HmModelPublishAppService hmModelPublishappService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseEntity<List<HmModelPublishResult>> list(@RequestParam Long modelId) {
        List<HmModelPublish> hmModelPublishList = hmModelPublishappService.getList(modelId);

        return ResponseEntity.ok(Convert.toList(HmModelPublishResult.class, hmModelPublishList));
    }

    @PutMapping("activate")
    @ApiOperation("激活版本")
    @Log("激活模型版本")
    public ResponseEntity<String> activate(@RequestBody IdInput input){
        hmModelPublishappService.activate(input.getId());
        return ResponseEntity.ok();
    }

    /**
     * 根据草稿创建版本
     */
    @PostMapping("by-script")
    @ApiOperation("根据草稿创建版本")
    @Log("根据草稿创建版本")
    public ResponseEntity<Long> byScript(@Valid @RequestBody PublishByScriptInput input, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        Long id = hmModelPublishappService.byScript(input.getId(),input.isActivated(),input.getRemark());

        return ResponseEntity.ok(id);
    }



    /**
     * 基于现有版本创建版本
     */
    @PostMapping("by-publish")
    @ApiOperation("基于现有版本创建版本")
    @Log("基于现有版本发布新版本")
    public ResponseEntity<Long> byPublish(@Valid @RequestBody PublishByPublishInput input, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        Long id = hmModelPublishappService.byPublish(input.isActivated(),input.getRemark(),input.getPublishId());

        return ResponseEntity.ok(id);
    }


    /**
     * 基于现有版本创建版本
     */
    @PostMapping("by-multi-version")
    @ApiOperation("跨版本创建版本")
    @Log("跨版本发布新版本")
    public ResponseEntity<Long> byMultiVersion(@Valid @RequestBody PublishByMultiVersionInput input, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        Long id = hmModelPublishappService.byMultiVersion(input.isActivated(),input.getRemark(),input.getEntityModelIds(),input.getModelId());

        return ResponseEntity.ok(id);
    }





}
